package com.group.eams.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.eams.pojo.dto.OpusCategoryQueryDto;
import com.group.eams.pojo.entity.OpusCategory;
import com.group.eams.service.OpusCategoryService;
import com.group.pojo.dto.BatchDeleteDto;
import com.group.result.Result;
import com.group.result.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 作品分类表(OpusCategory)表控制层
 *
 * @author DHUser
 * @since 2024-10-30 12:58:28
 */
@RestController
@RequestMapping("/opusCategory")
public class OpusCategoryController  {
    /**
     * 服务对象
     */
    @Autowired
    private OpusCategoryService opusCategoryService;

    /**
     * 通过id查询
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping(value = "/find",method = RequestMethod.GET)
    public Result findById(@RequestParam Long id){
        OpusCategory opusCategory = opusCategoryService.getById(id);
        return Result.buildSuccess(opusCategory);
    }

    /**
     * 分页查询
     *
     * @param opusCategoryQueryDto 分页查询对象
     * @return 分页数据
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result list(OpusCategoryQueryDto opusCategoryQueryDto){
        Page<OpusCategory> page = opusCategoryService.findByQueryDto(opusCategoryQueryDto);
        return Result.buildSuccess(page.getTotal(), page.getRecords());
    }

    /**
     * 查询所有
     *
     * @return 所有数据
     */
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Result findAll(){
        List<OpusCategory> list = opusCategoryService.list();
        return Result.buildSuccess(list);
    }

    /**
     * 新增数据
     *
     * @param opusCategory 实体对象
     * @return 新增结果
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody OpusCategory opusCategory){
        boolean result = opusCategoryService.save(opusCategory);
        return result ? Result.buildSuccess() : Result .buildFail(ResultEnum.OPTION_FAIL);
    }

    /**
     * 批量新增数据
     * @param opusCategoryList
     * @return 新增结果
     */
    @RequestMapping(value = "/addBatch",method = RequestMethod.POST)
    public Result addBatch(@RequestBody List<OpusCategory> opusCategoryList){
        boolean result = opusCategoryService.saveBatch(opusCategoryList);
        return result ? Result.buildSuccess() : Result .buildFail(ResultEnum.OPTION_FAIL);
    }

    /**
     * 修改数据
     *
     * @param opusCategory 实体对象
     * @return 修改结果
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(@RequestBody OpusCategory opusCategory){
        boolean result = opusCategoryService.updateById(opusCategory);
        return result ? Result.buildSuccess() : Result .buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/updateBatch",method = RequestMethod.POST)
    public Result updateBatch(@RequestBody List<OpusCategory> opusCategoryList){
        boolean result = opusCategoryService.updateBatchById(opusCategoryList);
        return result ? Result.buildSuccess() : Result .buildFail(ResultEnum.OPTION_FAIL);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public Result delete(@RequestParam Long id){
        boolean result = opusCategoryService.removeById(id);
        return result ? Result.buildSuccess() : Result .buildFail(ResultEnum.OPTION_FAIL);
    }


    /**
     * 批量删除数据
     * @param batchDeleteDto
     * @return 删除结果
     */
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.POST)
    public Result deleteBatch(@RequestBody BatchDeleteDto batchDeleteDto){
        boolean result = opusCategoryService.removeByIds(batchDeleteDto.getIds());
        return result ? Result.buildSuccess() : Result .buildFail(ResultEnum.OPTION_FAIL);
    }
    
}

